package com.chanyachting.emall.action.Emallservices;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Emallservices;
import com.chanyachting.emall.form.EmallservicesForm;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class EmallservicesAction extends DispatchAction {


	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("emallservicesList", Emallservices.list());
		
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward openForMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int memberId=Integer.parseInt(ru.getParameter("memberId", "0"));
		
		ru.setAttribute("memberId",memberId);  //保存会员编号
		
		ru.setAttribute("emallservicesList", Emallservices.list()); //保存功能服务列表
		
		return mapping.findForward("openForMember");
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			EmallservicesForm esForm=(EmallservicesForm)form;
			
			if(esForm.validate(mapping, request).isEmpty()){
				
				Emallservices es=new Emallservices();
				
				BeanUtils.copyProperties(es, esForm);
				
				es.setCreateDate(DateUtil.getDate());
				
				if(es.save()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
				
			}
			
		}
		
		return mapping.findForward("create");
	}
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			EmallservicesForm esForm=(EmallservicesForm)form;
			
			if(esForm.validate(mapping, request).isEmpty()){
				
				Emallservices es=new Emallservices(id); //先从数据库获取
				
				if(es.getId()!=0){
					
					BeanUtils.copyProperties(es, esForm);
					
					if(es.update()){
						
						ru.setSuccessMsg();
						
					}else{
						
						ru.setFailMsg();
					}

				}
				
			}
			
		}
		
		//重新加载
		ru.setAttribute("emallservices", new Emallservices(id) );
		
		return mapping.findForward("update");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Emallservices es=new Emallservices(id);
		
		if(es.getId()!=0){
			
			es.delete();
		}
		
		return mapping.findForward("delete");
	}
	
	
	

}
